<?PHP

 function validLang($lang) {
  return ctype_alpha($lang) && file_exists(dirname(__FILE__) . '/int/'.$lang.'.php');
 }

 function getLanguages() {
  $files = glob(dirname(__FILE__) . '/int/*.php');
  foreach ($files as $k => $v) {
   $files[$k] = substr($v, -6, 2);
  }
  return $files;
 }

 function getLanguageLink($l) {
  if (isset($_GET['lang']) && validLang($_GET['lang'])) {
   return str_replace('/' . $_GET['lang'] . '/', '/' . $l . '/', $_SERVER['REQUEST_URI']);
  } else {
   return preg_replace('#^(.*)(?<!documentation)/((documentation/)?.*?)$#', "\\1/$l/\\2", $_SERVER['REQUEST_URI']);
  }
 }

 if (!isset($_GET['lang']) || !validLang($_GET['lang'])) {
  if (date('m-d') == '09-19') {
   $lang = 'pr';
  } else {
   $lang = 'en';
  }
  //echo "Lang {$_GET['lang']} isn't valid. Redir." . getLanguageLink($lang);

  if (!defined('NOINT')) {
   header('Location: ' . getLanguageLink($lang));
   exit;
  } else {
   $_GET['lang'] = 'en';
  }
 }

 // Include english to fall back to
 require_once(dirname(__FILE__) . '/int/en.php');
 require_once(dirname(__FILE__) . '/int/'. $_GET['lang'] .'.php');

 $__dmdirc_trans = $trans;

 define('LANG', $_GET['lang']);

 function t($name) {
  global $__dmdirc_trans;

  if ($__dmdirc_trans == null) {
   // Wordpress ftl?
   require(dirname(__FILE__) . '/int/'. LANG .'.php');
   $__dmdirc_trans = $trans;
  }

  if (isset($__dmdirc_trans[$name])) {
   return $__dmdirc_trans[$name];
  } else {
   return '<span style="color: red;">' . $name . '</span>';
  }
 }

?>
